package com.ocom.human.controller;


import com.github.pagehelper.PageInfo;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityVisitorApprover;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.human.DeleteVisitorApproverVo;
import com.ocom.common.vo.human.GetVisitorApproverVo;
import com.ocom.human.service.VisitorApproverService;
//import com.ocom.human.vo.DeleteVisitorApproverVo;
//import com.ocom.human.vo.GetVisitorApproverVo;
//import com.ocom.human.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class VisitorApproverController {
    private final Logger log = LoggerFactory.getLogger(VisitorApproverController.class);

    @Autowired
    private VisitorApproverService visitorApproverService;

    /**
     * 获取审批负责人列表  (getVisitorApprover)
     * */
    @PostMapping(HumanServiceUrl.URL_GetVisitorApprover)
    public Result GetVisitorApprover(@Validated @RequestBody GetVisitorApproverVo getVisitorApproverVo){
        log.info("获取审批负责人列表请求参数{{}}",getVisitorApproverVo);
        Map<String, Object> map = new HashMap<>();
        try {
            List<EntityVisitorApprover> entityVisitorApprovers = visitorApproverService.GetVisitorApprover(getVisitorApproverVo.getComId(), getVisitorApproverVo);
            PageInfo<EntityVisitorApprover> entityVisitorApproverPageInfo = new PageInfo<>(entityVisitorApprovers);
            map.put("list",entityVisitorApprovers);
            map.put("total",entityVisitorApproverPageInfo.getTotal());
        }catch (Exception exception){
            log.error("获取审批负责人列表失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取审批负责人列表失败");
        }
        return ResultUtil.success(map);
    }

    /**
     * 添加审批负责人列表  (addVisitorApprover)
     * */
    @PostMapping(HumanServiceUrl.URL_AddVisitorApprover)
    public Result AddVisitorApprover(@Validated @RequestBody EntityVisitorApprover entityVisitorApprover){
        log.info("添加审批负责人列表请求参数{{}}",entityVisitorApprover);
        try {
            entityVisitorApprover.setPhone(StringUtils.desValue(entityVisitorApprover.getPhone(),3,4,"*"));
            visitorApproverService.AddVisitorApprover(entityVisitorApprover.getComId(), entityVisitorApprover);
        }catch (Exception exception){
            log.error("添加审批负责人列表失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"添加审批负责人列表失败");
        }
        return ResultUtil.success();
    }

    /**
     * 修改审批负责人列表  (updateVisitorApprover)
     * */
    @PostMapping(HumanServiceUrl.URL_UpdateVisitorApprover)
    public Result UpdateVisitorApprover(@Validated @RequestBody EntityVisitorApprover entityVisitorApprover){
        log.info("修改审批负责人列表请求参数{{}}",entityVisitorApprover);
        try {
            entityVisitorApprover.setPhone(StringUtils.desValue(entityVisitorApprover.getPhone(),3,4,"*"));
            visitorApproverService.UpdateVisitorApprover(entityVisitorApprover.getComId(), entityVisitorApprover);
        }catch (Exception exception){
            log.error("添加审批负责人列表失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"修改审批负责人列表失败");
        }
        return ResultUtil.success();
    }

    /**
     * 删除审批负责人列表  (deleteVisitorApprover)
     * */
    @PostMapping(HumanServiceUrl.URL_DeleteVisitorApprover)
    public Result DeleteVisitorApprover(@Validated @RequestBody DeleteVisitorApproverVo deleteVisitorApproverVo){
        log.info("删除审批负责人列表请求参数{{}}",deleteVisitorApproverVo);
        try {
            visitorApproverService.DeleteVisitorApprover(deleteVisitorApproverVo.getComId(), deleteVisitorApproverVo.getId());
        }catch (Exception exception){
            log.error("删除审批负责人列表失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"删除审批负责人列表失败");
        }
        return ResultUtil.success();
    }
}

